package com.fenqing.leetcode.code.t800_900;

import com.fenqing.annotations.Execute;
import com.fenqing.core.Topic;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author fenqing
 */
public class Topic841 extends Topic {
    /**
     * 构造器
     *
     * @param input 输入
     */
    public Topic841(String input) {
        super(input);
    }

    @Execute
    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        boolean[] opens = new boolean[rooms.size()];
        Deque<Integer> queue = new LinkedList<>();
        queue.offerLast(0);
        opens[0] = true;
        while (!queue.isEmpty()) {
            Integer index = queue.pollFirst();

            List<Integer> keys = rooms.get(index);
            for (Integer key : keys) {
                if (!opens[key]){
                    queue.offerLast(key);
                    opens[key] = true;
                }
            }
        }
        for (boolean open : opens) {
            if (!open) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        Topic841 topic841 = new Topic841("[[1,3],[3,0,1],[2],[0]]");
        boolean run = topic841.run();
        System.out.println(run);
    }
}
